package edu.uoc.tfc.web;

import java.lang.reflect.Constructor;

public class BeanCreator {
	
	public static final Object instance (String objectClass) {
		try {
			if (objectClass.indexOf("[]") > 0) {
				return null;
			}
			
			Class classInstance = Class.forName(objectClass);
			if (!classInstance.isInterface() && !classInstance.isPrimitive()){
				if (objectClass.indexOf("String") > 0) {
					return null;
				} else {
					Object object = null;
					if (hasEmptyConstructor(classInstance)) {
						object = classInstance.newInstance();
					}
					return object;
				}
			}
			
		} catch (Exception e) {
			//TODO: Tratar
		}
		return null;
	}
	
	private static boolean hasEmptyConstructor(Class classInstance) {
		Constructor[] constructors = classInstance.getConstructors();
		for (int i = 0; i < constructors.length; i++) {
			if (constructors[i].getParameterTypes().length == 0){
				return true;
			}
		}
		
		return false;
	}

}
